'''
Created on 2011/04/14

@author: Edgar Pereira adapted from code by Bruno
and http://www.velocityreviews.com/forums/t330554-kill-a-thread-in-python.html
'''


#Cria um servidor html, onde se pode colocar paginas html

import SimpleHTTPServer
import SocketServer
from KThread import *

class MyServer (threading.Thread):
    """Thread class with a stop() method. The thread itself has to check
    regularly for the stopped() condition."""

    #------------------------------------------------------------------------
    # Kills the thread (stop server)
    #------------------------------------------------------------------------
    def stop (self):
        self.server.kill()
        print "HTTP server stopped"
        pass
    pass


    #------------------------------------------------------------------------
    # return if the _stop.IsSet state
    #------------------------------------------------------------------------
    def stopped (self):
        return self._stop.isSet()
        pass
    pass
    
    
    #------------------------------------------------------------------------
    # Start the server on a KThread
    #------------------------------------------------------------------------
    def start (self):
        self.server = KThread(target=self.start_thread)
        self.server.start()
        pass
    pass


    #------------------------------------------------------------------------
    # http server
    #------------------------------------------------------------------------
    def start_thread (self):
        PORT = 8000 # port used by the http server
        Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
        httpd = SocketServer.TCPServer(('', PORT), Handler)        
        print "HTTP serving at port", PORT
        httpd.serve_forever()
        pass
    pass